//
//  YWHengVideoDetailController.m
//  videoTest3
//
//  Created by jun peng on 2022/6/17.
//

#import "YWHengVideoDetailController.h"
#import "YWHengVideoListController.h"
#import "YWHengVideoHeaderView.h"
#import "YWMenuStatus.h"
#import "YWControllerListStatus.h"
#import "YWHengVideoListController.h"

//必要的
#import "JXPagerView.h"
#import "JXCategoryTitleView.h"
#import "JXCategoryIndicatorLineView.h"
#import "JXCategoryView.h"
#import "JXPagerMainTableView.h"

//必要的
#import "YWMenuStatus.h"
#import "YWControllerListStatus.h"

#import "YWNavigationController.h"

#import "FullScreenGestureScrollView.h"
//#import <FDFullscreenPopGesture/UINavigationController+FDFullscreenPopGesture.h>
#import "UINavigationController+FDFullscreenPopGesture.h"

#import "YWGetNewsListParam.h"
#import "YWGetNewsListStatus.h"
#import "YWGetNewsResultStatus.h"
#import "YWGetNewsDataStatus.h"
#import "YWGetNewsTool3.h"

#import "YWHengAnimationVedioView.h"
#import "YWVideoView2.h"

#import "YWLittleWindowVedioView.h"

#import "AppDelegate.h"

#define categoryHeight 40 //栏目view高度
#define YWHeaderHeight 315


#define littleWindowInterval 5

//#define headerViewVedioViewHeight 210

@interface YWHengVideoDetailController ()<JXPagerViewDelegate,JXCategoryViewDelegate,JXPagerMainTableViewGestureDelegate,YWHengVideoHeaderViewDelegate,YWHengVideoListControllerDelegate,YWVideoView2Delegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *barConstraintHeight;

@property (nonatomic, strong) JXPagerView *pagerView;
@property (nonatomic, strong) JXCategoryTitleView *categoryView;

@property (nonatomic, strong) NSArray <NSString *> *titles;
@property (nonatomic,strong)NSMutableArray *menuArray;//装栏目的数组

@property (nonatomic, strong) NSMutableArray <YWHengVideoListController *> *listControllerArray;//换成装子控制器

@property (weak, nonatomic) IBOutlet UIView *bgView;


//mainTableView的headerView高度
@property(nonatomic,assign)CGFloat mainTableHeaderHeight;

@property(nonatomic,weak)YWHengVideoHeaderView *headerView;


//动画播放view
@property(nonatomic,strong)UIView *hengBgWhiteView;
@property(nonatomic,strong)YWHengAnimationVedioView *hengAnimationVedioView;
@property(nonatomic,assign)BOOL isGoToHengVideoDetailVCToDisAppearView;

@property (nonatomic, strong) UIView * vedioBgView; // 播放器BgView
@property (nonatomic, strong) YWVideoView2 * vedioView; // 播放器

//小窗口
@property(nonatomic,strong)YWLittleWindowVedioView *littleWindowVedioView;

//正在播放的头部选播视频
@property(nonatomic,strong)YWGetNewsDataStatus *selectedPlayingHeaderStatus;

//因为bgView的高度会包含categoryHeight、转换计算会有问题、所以增加了这个view计算
@property (weak, nonatomic) IBOutlet UIView *placeholderView;


//这个手势为了全屏时覆盖和阻止返回手势
@property(nonatomic,strong)UIPanGestureRecognizer *panGesture;

@end

@implementation YWHengVideoDetailController

-(UIPanGestureRecognizer *)panGesture{
    if (_panGesture == nil) {
        
        _panGesture = [[UIPanGestureRecognizer alloc] init];
        
        [self.view addGestureRecognizer:self.panGesture];
    }
    return _panGesture;
}

-(UIView *)hengBgWhiteView{
    if (_hengBgWhiteView == nil) {
        
        _hengBgWhiteView = [[UIView alloc] init];
        _hengBgWhiteView.frame = CGRectMake(0, 0, Wi, He);
        
        _hengBgWhiteView.backgroundColor = [UIColor whiteColor];
        _hengBgWhiteView.alpha = 0;
    }
    return _hengBgWhiteView;
}

-(YWHengAnimationVedioView *)hengAnimationVedioView{
    if (_hengAnimationVedioView == nil) {
        _hengAnimationVedioView = [YWHengAnimationVedioView videoView];
        
        _hengAnimationVedioView.backgroundColor = [UIColor blackColor];
    }
    return _hengAnimationVedioView;
}

-(YWLittleWindowVedioView *)littleWindowVedioView{
    if (_littleWindowVedioView == nil) {
        _littleWindowVedioView = [YWLittleWindowVedioView videoView];
        
        _littleWindowVedioView.backgroundColor = [UIColor blackColor];
        
        CGFloat w = Wi/2.5;
        CGFloat h = w/1.5;
        CGFloat y = self.view.height - h - littleWindowInterval - h;
        CGFloat x = Wi - w - littleWindowInterval;
        
        _littleWindowVedioView.frame = CGRectMake(x, y, w, h);
        
        [self.view addSubview:_littleWindowVedioView];
        
        _littleWindowVedioView.hidden = YES;
        
        _littleWindowVedioView.layer.cornerRadius = 5;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(littleWindowVedioViewTap)];
        
        [_littleWindowVedioView addGestureRecognizer:tap];
        
        
        UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
        [_littleWindowVedioView addGestureRecognizer:pan];
        
        
    }
    return _littleWindowVedioView;
}

-(void)pan:(UIPanGestureRecognizer *)pan{
 
    CGPoint transP = [pan translationInView:self.view];
    
    switch (pan.state) {
        case UIGestureRecognizerStateBegan:
        {

            
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            
            self.littleWindowVedioView.center = CGPointMake(self.littleWindowVedioView.centerX + transP.x, self.littleWindowVedioView.centerY + transP.y);
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
           
            if (self.littleWindowVedioView.centerX > Wi/2) {
                
                [UIView animateWithDuration:0.3 animations:^{
                    self.littleWindowVedioView.centerX = Wi - self.littleWindowVedioView.width/2 - littleWindowInterval;
              
                }];
                
            }
            else{
                [UIView animateWithDuration:0.3 animations:^{
                    self.littleWindowVedioView.centerX = self.littleWindowVedioView.width/2 + littleWindowInterval;
              
                }];
                
            }
            if (self.littleWindowVedioView.y < self.navBar.height + littleWindowInterval) {
                
                [UIView animateWithDuration:0.3 animations:^{
                    self.littleWindowVedioView.y = self.navBar.height + littleWindowInterval;
              
                }];
                
            }
            
            if (self.littleWindowVedioView.y > self.view.height - self.littleWindowVedioView.height - littleWindowInterval - 30) {
                
                [UIView animateWithDuration:0.3 animations:^{
                    self.littleWindowVedioView.y = self.view.height - self.littleWindowVedioView.height - littleWindowInterval - 30;
              
                }];
                
            }
            
           
        }
            break;
            
        default:
            break;
    }
    
    //复位.
    [pan setTranslation:CGPointZero inView:self.view];
    
}

//装menu字符串数组
-(NSArray*)titles{
    if (_titles == nil) {
        _titles = [NSArray array];
    }
    return _titles;
}

//装menu模型的数组
-(NSMutableArray*)menuArray{
    if (_menuArray == nil) {
        _menuArray = [NSMutableArray array];
    }
    return _menuArray;
}

//装子控制器
-(NSMutableArray*)listControllerArray{
    if (_listControllerArray == nil) {
        _listControllerArray = [NSMutableArray array];
    }
    return _listControllerArray;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navBar.title = @"视频详情";
    self.barConstraintHeight.constant = self.navBar.frame.size.height;
    
    //顺序：headerView - pagerView - vedioView - 播放
    
    //headerView
    YWHengVideoHeaderView *header = [YWHengVideoHeaderView headerView];
    self.mainTableHeaderHeight = YWHeaderHeight; //先给个默认值、到时变化好一点
    header.frame = CGRectMake(0, 0, Wi, self.mainTableHeaderHeight);
    header.delegate = self;
    self.headerView = header;
    self.headerView.delegate = self;
    self.headerView.status = self.status; //赋值模型
    
    
    
     //先加载栏目数据 - 创建pager - 加载mainTableView数据 - 子VC beginLoadData
    [self loadMenuData];
//    [self setupRightItem];
    [self loadNewsData];
    
    
    // 创建播放器
    self.vedioView = [YWVideoView2 videoView];
    self.vedioView.delegate = self;
    self.vedioBgView = [UIView new];
    [self.pagerView.mainTableView addSubview:self.vedioBgView];
    self.vedioBgView.backgroundColor = [UIColor blackColor];
    self.vedioView.backgroundColor = [UIColor blackColor];
    self.vedioBgView.hidden = YES; //隐藏
    [self.vedioBgView addSubview:self.vedioView];
    self.vedioView.frame = self.vedioBgView.bounds;
    
    //这里暂时转换不了
    //播放器在headerView位置播放
//    CGRect rects = [self.headerView convertRect:self.headerView.videoBgView.frame toView:self.pagerView.mainTableView];
//    YWLog(@"rect:%@",NSStringFromCGRect(rects));
//    YWLog(@"width:%f",Wi);
//    self.vedioBgView.frame = rects;
    //只能暂时用self.headerView.videoBgView.frame的默认值
    self.vedioBgView.frame = CGRectMake(0, 0, Wi, headerViewVedioViewHeight);
    self.vedioView.frame = self.vedioBgView.bounds;
    self.vedioView.index = -1;  //headerView播放的默认index是-1
    self.vedioView.videoStatusType = YWVideoLocationType_headerView;//设置videoStatusType
    self.vedioBgView.hidden = NO;//显示
    
    //指着保存
    self.selectedPlayingHeaderStatus = self.status;
    [self.vedioView continuePlayWithAnthorViewWithUrl:self.status.FileUrl WithMute:NO];//继续在headerView播放

    
}



-(void)pagerView:(JXPagerView *)pagerView mainTableViewDidScroll:(UIScrollView *)scrollView{
    
    YWLog(@"aaa === %f",scrollView.contentOffset.y);
    
    YWLog(@"scrollView.contentOffset.y:%f",scrollView.contentOffset.y);
    
    //这里用isPlaying不用isPlaying_containPause
    if (self.vedioView.isPlaying == YES && self.vedioView.videoStatusType == YWVideoLocationType_headerView) {  //headerView正在播放中
      
        if (scrollView.contentOffset.y >= headerViewVedioViewHeight) {
            
            //这样就可以保证只执行一次、因为你滚动过程不断掉用continue会造成卡顿的
            if (self.littleWindowVedioView.hidden == YES) {
                
                self.littleWindowVedioView.hidden = NO;
                
                [self.littleWindowVedioView continuePlayWithAnthorViewWithUrl:self.vedioView.urlString WithMute:NO];
            }
            
        }
        else{
            
            //这样就可以保证只执行一次、因为你滚动过程不断掉用continue会造成卡顿的
            if (self.littleWindowVedioView.hidden == NO) {
                
                self.littleWindowVedioView.hidden = YES;
                
                [self.vedioView continuePlayWithAnthorViewWithUrl:self.vedioView.urlString WithMute:NO];
            }
            
         
        }
    }
    //下面的只能写在子VC的scrollViewDidScroll里
    /*
    else{ //不是headerView或者正在播放中的
        
        self.littleWindowVedioView.hidden = YES;
        
        if (self.vedioView.isPlaying_containPause == YES &&  self.vedioView.videoStatusType == YWVideoLocationType_leftVC ) { //左VC正在播放中
        
            YWHengVideoListController *vc = self.listControllerArray[0];
            //正在播放状态

               NSInteger index = self.vedioView.index;
               
               
               NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
               
            id cell = [vc.tableView cellForRowAtIndexPath:indexPath];
               
           YWLog(@"%@",[cell class]);
           
           
           //点击更多、是重新刷新的过程、由于有些文章太长、导致下面的本来正在播放视频cell已经超出了屏幕的可是范围，而根据indexPath拿回之前的视频cell是null的（reloadData之后cell还没有创建出来、要第一次见到cell、cell才会被创建），所以要写下面的两步、避免出现vedioView在长文章上面
           if ([cell isKindOfClass:[NSNull class]] || cell == nil) {
               
               [self.vedioView stopPlay];
               self.vedioBgView.hidden = YES;
               
               
               return;
           }
           
           if ([cell isKindOfClass:[YWHomeRecommendVedioCell class]]) {
               YWHomeRecommendVedioCell *vedioCell = (YWHomeRecommendVedioCell *)cell;
               
               
//其实这里重新赋值rect可能不需要、因为本来就是add在了tableView上
               //转换父控件所在位置
               CGRect rects = [vedioCell.contentView convertRect:vedioCell.videoBgView.frame toView:vc.tableView];

               //不断改变vedioBgView位置
               self.vedioBgView.frame = rects;

               self.vedioView.frame = self.vedioBgView.bounds;

               YWLog(@"rects：%@",NSStringFromCGRect(rects));
               
           //    判断播放视频的cell是否离开了屏幕
               CGRect cellRect = [vedioCell.contentView convertRect:vedioCell.videoBgView.frame toView:self.bgView];
               
               if ((cellRect.origin.y < -cellRect.size.height + categoryHeight) || (cellRect.origin.y > self.bgView.height)) {
                   [self.vedioView stopPlay];
                   self.vedioBgView.hidden = YES;
               }
               
               YWLog(@"%@",NSStringFromCGRect(cellRect));
               
               
               YWLog(@"aaa ---------");
               YWLog(@"aaa - cellRect.origin.y:%f",cellRect.origin.y);
               YWLog(@"aaa - -cellRect.size.height + categoryHeight:%f",-cellRect.size.height + categoryHeight);
               YWLog(@"aaa - self.bgView.height:%f", self.bgView.height);
               YWLog(@"aaa ---------");

           }
        
        }
        
        
        if (self.vedioView.isPlaying_containPause == YES &&  self.vedioView.videoStatusType == YWVideoLocationType_rightVC ) { //右VC正在播放中
            
            YWHengVideoListController *vc = self.listControllerArray[1];
        
            
                   NSInteger index = self.vedioView.index;
                   
                   
                   NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
                   
                id cell = [vc.tableView cellForRowAtIndexPath:indexPath];
                   
               YWLog(@"%@",[cell class]);
               
               
               //点击更多、是重新刷新的过程、由于有些文章太长、导致下面的本来正在播放视频cell已经超出了屏幕的可是范围，而根据indexPath拿回之前的视频cell是null的（reloadData之后cell还没有创建出来、要第一次见到cell、cell才会被创建），所以要写下面的两步、避免出现vedioView在长文章上面
               if ([cell isKindOfClass:[NSNull class]] || cell == nil) {
                   
                   [self.vedioView stopPlay];
                   self.vedioBgView.hidden = YES;
                   
                   
                   return;
               }
               
               if ([cell isKindOfClass:[YWHomeRecommendVedioCell class]]) {
                   YWHomeRecommendVedioCell *vedioCell = (YWHomeRecommendVedioCell *)cell;
                   
                   
    //其实这里重新赋值rect可能不需要、因为本来就是add在了tableView上
                   //转换父控件所在位置
                   CGRect rects = [vedioCell.contentView convertRect:vedioCell.videoBgView.frame toView:vc.tableView];
    
                   //不断改变vedioBgView位置
                   self.vedioBgView.frame = rects;
    
                   self.vedioView.frame = self.vedioBgView.bounds;
    
                   YWLog(@"rects：%@",NSStringFromCGRect(rects));
                   
               //    判断播放视频的cell是否离开了屏幕
                   CGRect cellRect = [vedioCell.contentView convertRect:vedioCell.videoBgView.frame toView:self.bgView];
                   
                   if ((cellRect.origin.y < -cellRect.size.height + categoryHeight) || (cellRect.origin.y > self.bgView.height)) {
                       [self.vedioView stopPlay];
                       self.vedioBgView.hidden = YES;
                   }
                   
                   YWLog(@"%@",NSStringFromCGRect(cellRect));

               }
            
            }
        
    }
    */
}

-(void)hengVideoListController:(YWHengVideoListController *)vc scrollViewDidScroll:(UIScrollView *)scrollView{
    
    YWLog(@"scrollView.contentoffsett1:%f",scrollView.contentOffset.y);
    
    
//    if (vc.videoStatusType == YWVideoLocationType_leftVC) {
//        YWLog(@"aaff");
//    }
    
    //我这里只判断滚动过程中不是headerView在播放、假如是左VC在滚动、右VC是不会走这代理、同理右VC在滚动左VC也不会走这个代理
    if (self.vedioView.isPlaying_containPause == YES &&  self.vedioView.videoStatusType != YWVideoLocationType_headerView ) {

         //正在播放状态

            NSInteger index = self.vedioView.index;


            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];

            id cell = [vc.tableView cellForRowAtIndexPath:indexPath];

        YWLog(@"%@",[cell class]);


        //点击更多、是重新刷新的过程、由于有些文章太长、导致下面的本来正在播放视频cell已经超出了屏幕的可是范围，而根据indexPath拿回之前的视频cell是null的（reloadData之后cell还没有创建出来、要第一次见到cell、cell才会被创建），所以要写下面的两步、避免出现vedioView在长文章上面
        if ([cell isKindOfClass:[NSNull class]] || cell == nil) {

            [self.vedioView stopPlay];
            self.vedioBgView.hidden = YES;


            return;
        }

        if ([cell isKindOfClass:[YWHomeRecommendVedioCell class]]) {
            YWHomeRecommendVedioCell *vedioCell = (YWHomeRecommendVedioCell *)cell;

//其实这里重新赋值rect可能不需要、因为本来就是add在了tableView上
            //转换父控件所在位置
            CGRect rects = [vedioCell.contentView convertRect:vedioCell.videoBgView.frame toView:vc.tableView];

            //不断改变vedioBgView位置
            self.vedioBgView.frame = rects;

            self.vedioView.frame = self.vedioBgView.bounds;

            YWLog(@"rects：%@",NSStringFromCGRect(rects));

        //    判断播放视频的cell是否离开了屏幕
            CGRect cellRect = [vedioCell.contentView convertRect:vedioCell.videoBgView.frame toView:self.placeholderView];

            //增加了一个条件不是横屏（因为有时切换为横屏时候cell离开了屏幕导致播放器被stop了）、、、但是假如是横屏切换到竖屏有时候也会走这里、因为tableView的高度由矮变高过程cell会可能离开屏幕，所以对于本VC使用该控件Pager暂时无法保证横屏切换到竖屏可以继续播放而不走这里、、、除非你又增加一些属性表明当前是横屏切换成竖屏状态而不走这里之类的、这里先暂时不理了
            if (((cellRect.origin.y < -cellRect.size.height) || (cellRect.origin.y > self.placeholderView.height)) && !isLanscape) {
                [self.vedioView stopPlay];
                self.vedioBgView.hidden = YES;
            }

            YWLog(@"%@",NSStringFromCGRect(cellRect));

        }
     

    }
   
}


-(void)hengVideoListController:(YWHengVideoListController *)vc scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
        if (decelerate == YES) {
    
    //        [self scrollToStartPlay:scrollView]; //这个和下面的减速可能冲突会出现滚动播放卡顿所以这个不要
        }
        else{
    
            [self scrollToStartPlay:scrollView];
        }
    
    YWLog(@"scrollView.contentoffsett2:%f",scrollView.contentOffset.y);
}


-(void)hengVideoListController:(YWHengVideoListController *)vc scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    [self scrollToStartPlay:scrollView];
    
    YWLog(@"scrollView.contentoffsett3:%f",scrollView.contentOffset.y);
}


/*
//- (void)pagerView:(JXPagerView *)pagerView mainTableViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//
//    if (decelerate == YES) {
//
////        [self scrollToStartPlay:scrollView]; //这个和下面的减速可能冲突会出现滚动播放卡顿所以这个不要
//    }
//    else{
//
//        [self scrollToStartPlay:scrollView];
//    }
//
//
//    YWLog(@"aaa:scrollViewDidEndDragging");
//}
//- (void)pagerView:(JXPagerView *)pagerView mainTableViewDidEndDecelerating:(UIScrollView *)scrollView{
//
//    [self scrollToStartPlay:scrollView];
//
//    YWLog(@"aaa:scrollViewDidEndDecelerating");
//}
*/

//滚动过程中寻找可播放的cell （可以包含headerView）//scrollView是子VC的tableView
-(void)scrollToStartPlay:(UIScrollView *)scrollView{
    
    YWLog(@"aaa:scrollToStartPlay");
    
    if(self.vedioView.isPlaying_containPause == NO)  {
        
        //假如之前是头部在播放的
        if (self.vedioView.videoStatusType == YWVideoLocationType_headerView) {
          
            //注意上面的scrollView是子VC的tableView、所以这里用self.pagerView.mainTableView
            if (self.pagerView.mainTableView.contentOffset.y >= headerViewVedioViewHeight) {
                self.littleWindowVedioView.hidden = NO;
                
                self.vedioView.index = -1;
                self.vedioView.videoStatusType = YWVideoLocationType_headerView;
                
                [self.littleWindowVedioView startPlayWithUrl:self.selectedPlayingHeaderStatus.FileUrl withMute:NO];
               
            }
            else{
                
                self.littleWindowVedioView.hidden = YES;
                
                self.vedioView.index = -1;
                self.vedioView.videoStatusType = YWVideoLocationType_headerView;
                
                [self.vedioView startPlayWithUrl:self.selectedPlayingHeaderStatus.FileUrl withMute:NO];
             
            }
        }
        else{ //这里是“之前是”非头部的播放、即“之前是”子VC中的一个在播放
            
            if (self.categoryView.selectedIndex == 0) {
                
                YWHengVideoListController *vc = self.listControllerArray[0];
                
        //          暂停播放状态/没有播放状态
        //        遍历可见到的cell，假如是视频的就直接播放，获取到第一个视频cell就记得要return

                NSArray *cellArray = [vc.tableView visibleCells];
                
                YWLog(@"cellArray_count:%ld",cellArray.count);

                  for (id cell in cellArray) {
                      
                    if ([cell isKindOfClass:[YWHomeRecommendVedioCell class]]) {
                         
                        YWHomeRecommendVedioCell *vedioCell = (YWHomeRecommendVedioCell *)cell;
                        
                          NSIndexPath *indexPath = [vc.tableView indexPathForCell:vedioCell];


                          //播放器中心距离bgView中心在一定距离才能播放
                          CGRect cellRect = [vedioCell.contentView convertRect:vedioCell.videoBgView.frame toView:self.placeholderView];
                          
                        CGFloat videoCenterY = cellRect.origin.y + cellRect.size.height/2;
                        CGFloat bgViewCenterY = self.placeholderView.height/2;
                        
                        CGFloat absY = fabs(videoCenterY - bgViewCenterY);
                        
                        //absY > fabs(bgViewCenterY - cellRect.size.height/2这个意思是整个videoView都要露出来才播放
                        if (absY > fabs(bgViewCenterY - cellRect.size.height/2)) { //假如距离太远就寻找下一个播放器
                            continue;
                        }
                          
                          
                          //转换父控件所在位置
                          CGRect rects = [vedioCell.contentView convertRect:vedioCell.videoBgView.frame toView:vc.tableView];

                          self.vedioBgView.frame = rects;

                          self.vedioView.frame = self.vedioBgView.bounds;

                          self.vedioView.index = indexPath.row;

                          YWGetNewsDataStatus *vedioStatus = vc.statusArray[indexPath.row];
                        
                        
                        self.vedioView.videoStatusType = YWVideoLocationType_leftVC;
        //这里按理mute是YES、但是这里先测试设置NO
                          [self.vedioView startPlayWithUrl:vedioStatus.FileUrl withMute:NO];
                        
                        //添加位置
                        [vc.tableView addSubview:self.vedioBgView];


                          self.vedioBgView.layer.cornerRadius = vedioCell.videoBgView.layer.cornerRadius;
                          self.vedioBgView.clipsToBounds = YES;

                           //GCD延迟
                          dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                                     self.vedioBgView.hidden = NO;
                              
                          });


                        //滚动播放不做移动cell位置了、点击才做
//                        [vc scrollToIndexRow:self.vedioView.index];
                        
                          break;
                      }
                        
                  }
            
            }
            else  if (self.categoryView.selectedIndex == 1){
                
                YWHengVideoListController *vc = self.listControllerArray[1];
            
                
        //          暂停播放状态/没有播放状态
        //        遍历可见到的cell，假如是视频的就直接播放，获取到第一个视频cell就记得要return

                NSArray *cellArray = [vc.tableView visibleCells];
                
                YWLog(@"cellArray_count:%ld",cellArray.count);

                  for (id cell in cellArray) {
                      
                    if ([cell isKindOfClass:[YWHomeRecommendVedioCell class]]) {
                         
                        YWHomeRecommendVedioCell *vedioCell = (YWHomeRecommendVedioCell *)cell;
                        
                          NSIndexPath *indexPath = [vc.tableView indexPathForCell:vedioCell];


                          //播放器中心距离bgView中心在一定距离才能播放
                          CGRect cellRect = [vedioCell.contentView convertRect:vedioCell.videoBgView.frame toView:self.placeholderView];
                          
                        CGFloat videoCenterY = cellRect.origin.y + cellRect.size.height/2;
                        CGFloat bgViewCenterY = self.placeholderView.height/2;
                        
                        CGFloat absY = fabs(videoCenterY - bgViewCenterY);
                        
                        //absY > fabs(bgViewCenterY - cellRect.size.height/2这个意思是整个videoView都要露出来才播放
                        if (absY > fabs(bgViewCenterY - cellRect.size.height/2)) { //假如距离太远就寻找下一个播放器
                            continue;
                        }
                          
                          
                          //转换父控件所在位置
                          CGRect rects = [vedioCell.contentView convertRect:vedioCell.videoBgView.frame toView:vc.tableView];

                          self.vedioBgView.frame = rects;

                          self.vedioView.frame = self.vedioBgView.bounds;

                          self.vedioView.index = indexPath.row;

                          YWGetNewsDataStatus *vedioStatus = vc.statusArray[indexPath.row];
                        
                        self.vedioView.videoStatusType = YWVideoLocationType_rightVC;
        //这里按理mute是YES、但是这里先测试设置NO
                          [self.vedioView startPlayWithUrl:vedioStatus.FileUrl withMute:NO];
                        
                        
                        //添加位置
                        [vc.tableView addSubview:self.vedioBgView];

                          self.vedioBgView.layer.cornerRadius = vedioCell.videoBgView.layer.cornerRadius;
                          self.vedioBgView.clipsToBounds = YES;

                           //GCD延迟
                          dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                                     self.vedioBgView.hidden = NO;
                              });


                        //滚动播放不做移动cell位置了、点击才做
//                        [vc scrollToIndexRow:self.vedioView.index];

                          break;
                      }
                        
                  }
            
                
            }
            
            
            
            
        }
        
        
        
        

    }
    
}



-(void)littleWindowVedioViewTap{
    
    [self pagerViewScrollToTop];
   
}


- (void)pagerViewScrollToTop{
    //必须让self.pagerView.currentScrollingListView滚动到顶部，才能触发main列表的滚动所以这里的animated必须是NO
    [self.pagerView.currentScrollingListView setContentOffset:CGPointZero animated:NO];
    //然后让self.pagerView.mainTableView滚动到顶部，这里的animated可以为YES
    [self.pagerView.mainTableView setContentOffset:CGPointZero animated:YES];
}


//点击了子VC的播放器要告诉一下主VC (传cell为了转换frame由主VC完成) （播放）
-(void)hengVideoViewInListVCDidTap:(YWHengVideoListController *)controller cell:(YWHomeRecommendVedioCell *)cell withStatus:(YWGetNewsDataStatus *)status videoStatusType:(YWVideoLocationType)videoStatusType vedioIndex:(NSInteger)index{
    
    self.littleWindowVedioView.hidden = YES;
    
    self.vedioView.videoStatusType = videoStatusType;
    
    self.vedioView.index = index;
    
    [self.vedioView startPlayWithUrl:status.FileUrl withMute:NO];
    
    //不是左就是右边VC
    if (videoStatusType == YWVideoLocationType_leftVC) {
        YWHengVideoListController *listVC = self.listControllerArray[0];
        
        //添加位置
        [listVC.tableView addSubview:self.vedioBgView];
        
        //cell转换位置
        CGRect rects = [cell.contentView convertRect:cell.videoBgView.frame toView:listVC.tableView];
        
        self.vedioBgView.frame = rects;
        
        self.vedioView.frame = self.vedioBgView.bounds;

        self.vedioBgView.layer.cornerRadius = cell.videoBgView.layer.cornerRadius;
        self.vedioBgView.clipsToBounds = YES;

        
        self.vedioBgView.hidden = NO;
        
        //点击做移动cell位置到中间
        [listVC scrollToIndexRow:self.vedioView.index];
        
    }
    else if (videoStatusType == YWVideoLocationType_rightVC){
        YWHengVideoListController *listVC = self.listControllerArray[1];
        
        
        //添加位置
        [listVC.tableView addSubview:self.vedioBgView];
        
        //cell转换位置
        CGRect rects = [cell.contentView convertRect:cell.videoBgView.frame toView:listVC.tableView];
        
        
        self.vedioBgView.frame = rects;
        
        self.vedioView.frame = self.vedioBgView.bounds;

        self.vedioBgView.layer.cornerRadius = cell.videoBgView.layer.cornerRadius;
        
        self.vedioBgView.clipsToBounds = YES;

        
        self.vedioBgView.hidden = NO;
        
        //点击做移动cell位置到中间
        [listVC scrollToIndexRow:self.vedioView.index];
        
    }
    
    
   
    
}

//点击了子VC的Cell要告诉一下主VC (传cell为了转换frame由主VC完成) （去下一页）
-(void)hengCellDidTap:(YWHengVideoListController *)controller cell:(YWHomeRecommendVedioCell *)cell withStatus:(YWGetNewsDataStatus *)status videoStatusType:(YWVideoLocationType)videoStatusType vedioIndex:(NSInteger)index{
    
    self.littleWindowVedioView.hidden = YES;
    
    self.vedioView.videoStatusType = videoStatusType;
    
    self.vedioView.index = index;
    
    //是不是因为去横视频详情VC而离开本界面
    self.isGoToHengVideoDetailVCToDisAppearView = YES;
    
//看URL是否一致、是的话就continue、不是的话就start
    //1、hengAnimationVedioView开始播放/继续播放
    if ([self.vedioView.urlString isEqualToString:status.FileUrl]) {
        
        [self.hengAnimationVedioView continuePlayWithAnthorViewWithUrl:status.FileUrl WithMute:NO];
    }
    else{
        [self.hengAnimationVedioView startPlayWithUrl:status.FileUrl withMute:NO];
    }
    
    //2、添加
    [[UIApplication sharedApplication].keyWindow addSubview:self.hengBgWhiteView];
    [[UIApplication sharedApplication].keyWindow addSubview:self.hengAnimationVedioView];
    
//这里利用给你的cell来转换frame
    //3、初始化位置d
    CGRect startRect = [cell.contentView convertRect:cell.videoBgView.frame toView:[UIApplication sharedApplication].keyWindow];
    
    self.hengAnimationVedioView.frame = startRect;
    
    //4、动画
    [UIView animateWithDuration:0.3 animations:^{
        
        self.hengBgWhiteView.alpha = 1;
        self.hengAnimationVedioView.frame = CGRectMake(0, self.navBar.height, Wi, headerViewVedioViewHeight);
    }completion:^(BOOL finished) {
        
        //5、弹出新的VC
        YWHengVideoDetailController *vc = [[YWHengVideoDetailController alloc] initWithNibName:@"YWHengVideoDetailController" bundle:nil];
//        vc.delegate = self;
        vc.status = status;
        
        [self.navigationController pushViewController:vc animated:NO];
        
        //6、移除动画view
        self.hengBgWhiteView.alpha = 0;
        [self.hengAnimationVedioView removeFromSuperview];
        [self.hengBgWhiteView removeFromSuperview];
        
        
    }];
    
    
    
    
}

//点击头部播放
-(void)hengVideoHeaderViewDidTap:(YWHengVideoHeaderView *)headerView withStatus:(YWGetNewsDataStatus *)status{
    
    //这里有可能是点击选播传过来的、所以要保存
    self.selectedPlayingHeaderStatus = status;
    
    self.littleWindowVedioView.hidden = YES;
    
    [self.vedioView startPlayWithUrl:status.FileUrl withMute:NO];
    
    self.vedioView.videoStatusType = YWVideoLocationType_headerView;
    
    self.vedioView.index = -1;
    
    [self.pagerView.mainTableView addSubview:self.vedioBgView];
    
    self.vedioBgView.frame = CGRectMake(0, 0, Wi, headerViewVedioViewHeight);
    
    self.vedioView.frame = self.vedioBgView.bounds;

    self.vedioBgView.layer.cornerRadius = 0;
    
    self.vedioBgView.clipsToBounds = YES;

    self.vedioBgView.hidden = NO;
}




-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    
    //强制竖屏
    [self setViewOrientation:UIInterfaceOrientationPortrait];
    
    
//    [self.vedioView pausePlay];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
//    [self.vedioView continuePlay];
}



-(void)loadNewsData{
    
    NSInteger page = 1;
    NSInteger remarks = 2;
    YWGetNewsListParam *param = [[YWGetNewsListParam alloc] init];
    param.pagesize = @"15";
    param.currentpage = [NSString stringWithFormat:@"%ld",(long)page];
    
    param.type = [NSString stringWithFormat:@"%ld",(long)remarks];
    NSString *userID = self.meStatus.userStatus.Id;
      if (userID != nil) {
           param.userid = userID;
       }
       else{
           
           param.userid = @"";
       }
    
    [YWGetNewsTool3 getNewsListStatusesWithParameters:param success:^(YWGetNewsListStatus * _Nonnull status) {
        
               if (status.IsSuccess.integerValue == 1) {
                   
                   self.headerView.statusListArray = status.ResultObj.PageInfo;
                   
               }
               else{
                   
                   self.headerView.statusListArray = [NSMutableArray array];
                   
               }
    } failure:^(NSError * _Nonnull error) {
        
    }];
    
    
}
//-(void)setupRightItem{
//
//    UIButton *rightItem = [[UIButton alloc] init];
//    [rightItem setTitle:@"切换" forState:UIControlStateNormal];
//    [rightItem setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    rightItem.bounds = CGRectMake(0, 0, 180, 44);
//    rightItem.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
//    [rightItem setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 8)];
//    self.navBar.rightItem = rightItem;
//    rightItem.titleLabel.font = [UIFont systemFontOfSize:14];
//    [rightItem addTarget:self action:@selector(didNaviRightItemClick) forControlEvents:UIControlEventTouchUpInside];
//}


//- (void)didNaviRightItemClick {
//    if (self.pagerView.pinSectionHeaderVerticalOffset == self.mainTableHeaderHeight) {
//        self.pagerView.pinSectionHeaderVerticalOffset = 0;
//    }else {
//        self.pagerView.pinSectionHeaderVerticalOffset = self.mainTableHeaderHeight;
//    }
////    [self.pagerView reloadData];
//}

-(void)loadMenuData{

    YWMenuStatus *vedio = [[YWMenuStatus alloc] init];
    vedio.title = @"视频1";
    vedio.remarks = 2;   //视频必须是2、因为接口要根据这个
    
    
    YWMenuStatus *vedio2 = [[YWMenuStatus alloc] init];
    vedio2.title = @"视频2";
    vedio2.remarks = 2;   //视频必须是2、因为接口要根据这个
    
    self.menuArray = [NSMutableArray arrayWithArray:@[vedio,vedio2]];
    
    //titles
    NSMutableArray *titles = [NSMutableArray array];
    
    for (NSInteger i = 0; i < self.menuArray.count; i++) {
        
        YWMenuStatus *menuStatus = self.menuArray[i];
        
        [titles addObject:menuStatus.title];
    }
    
    self.titles = titles;
    
    //建立控制器数组
    [self setupListViews];
    
    
}



//建立控制器数组
-(void)setupListViews{
    
    NSMutableArray *listControllerArray = [NSMutableArray array];
    
    for (NSInteger i = 0; i < self.menuArray.count; i++) {
        
        YWMenuStatus *menuStatus = self.menuArray[i];
        
        switch (menuStatus.remarks) {
          
            case 2:
            {
                
                //视频的 应该不需要用YWHomeVedioListVC了、我直接用回YWHomeListVC2就行、我remark传2给后台，它只返回视频给我就行
                
                YWHengVideoListController *listViewVC = [[YWHengVideoListController alloc] initWithNibName:@"YWHengVideoListController" bundle:nil];

                listViewVC.delegate = self;
                
                listViewVC.videoStatusType = i + 1;

                //这里要增加加入子控制器、不然在pager的结构下不能在子VC push到下一个控制器
                [self addChildViewController:listViewVC];

                YWControllerListStatus *listVCStatus = [[YWControllerListStatus alloc] init];

                  listVCStatus.remarks = menuStatus.remarks;

                  listViewVC.status = listVCStatus;


                  [listControllerArray addObject:listViewVC];

            }
                break;

            default:
                break;
        }
        
    }
    
    //每个控制器的view添加到数组里
    self.listControllerArray = listControllerArray;
    
    [self setupPageListView];
    
}


//创建pageListView
-(void)setupPageListView{
    
    //pagerView创建
    self.pagerView = [[JXPagerView alloc] initWithDelegate:self listContainerType:JXPagerListContainerType_ScrollView];
    
    //当加了这两句的时候、上下滚动pagerView就不会产生手势返回、这问题就解决了
    [((YWNavigationController *)(self.navigationController)).pan requireGestureRecognizerToFail:self.pagerView.listContainerView.scrollView.panGestureRecognizer];
    [((YWNavigationController *)(self.navigationController)).pan requireGestureRecognizerToFail:self.pagerView.mainTableView.panGestureRecognizer];
    
    self.pagerView.mainTableView.bounces = NO;
    
    //设置手势代理是本VC处理
    self.pagerView.mainTableView.gestureDelegate = self;
    [self.bgView addSubview:self.pagerView];
    
    //这里可以反映控件层次结构
    self.bgView.backgroundColor = [UIColor blackColor];
    self.pagerView.backgroundColor = [UIColor whiteColor];
//    self.headerView.backgroundColor = [UIColor whiteColor];
    self.pagerView.mainTableView.backgroundColor = [UIColor whiteColor];
    self.pagerView.listContainerView.backgroundColor = [UIColor whiteColor];
   
    //categoryView创建
    _categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, Wi, categoryHeight)];
    self.categoryView.titles = self.titles;
    self.categoryView.backgroundColor = [UIColor whiteColor];
    self.categoryView.titleSelectedColor = YWColor(36, 36, 36);/// mainColor;
    self.categoryView.titleColor = [UIColor lightGrayColor];
    self.categoryView.titleColorGradientEnabled = YES;
    self.categoryView.titleLabelZoomEnabled = YES;
    self.categoryView.contentScrollViewClickTransitionAnimationEnabled = NO;
    self.categoryView.delegate = self;
    self.categoryView.titleFont = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
//    self.categoryView.titleSelectedFont = [UIFont systemFontOfSize:17 weight:UIFontWeightMedium];
    
    CGFloat interval = 15;
    CGFloat cellWidth = (Wi - 15)/8;
    CGFloat cellSpacing = 10;
    self.categoryView.cellSpacing = cellSpacing;
    self.categoryView.contentEdgeInsetLeft = interval;
    self.categoryView.contentEdgeInsetRight = cellWidth * 6 - cellSpacing ;
    self.categoryView.cellWidth = cellWidth;
    
    //默认选中
    self.categoryView.defaultSelectedIndex = 0;
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.indicatorColor = mainColor;
    lineView.indicatorWidth = 20;
    lineView.indicatorHeight = 4;
    lineView.indicatorCornerRadius = 2;
    lineView.verticalMargin = 10;
    self.categoryView.indicators = @[lineView];

    self.categoryView.listContainer = (id<JXCategoryViewListContainer>)self.pagerView.listContainerView;

    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, categoryHeight - 1, Wi, 1)];
    line.backgroundColor = YWColor(245, 245, 245);
    [self.categoryView addSubview:line];
    

    
}




//////////////////////**************///////////////////////////

#pragma mark - JXPagerViewDelegate

- (UIView *)tableHeaderViewInPagerView:(JXPagerView *)pagerView {
    return self.headerView;
}

- (NSUInteger)tableHeaderViewHeightInPagerView:(JXPagerView *)pagerView {
    return self.mainTableHeaderHeight;
}

- (NSUInteger)heightForPinSectionHeaderInPagerView:(JXPagerView *)pagerView {
    return categoryHeight;
}

- (UIView *)viewForPinSectionHeaderInPagerView:(JXPagerView *)pagerView {
    return self.categoryView;
}

- (NSInteger)numberOfListsInPagerView:(JXPagerView *)pagerView {
    //和categoryView的item数量一致
    return self.categoryView.titles.count;
}

//有新的VC出现或者刷新pagerView都会来这个方法
- (id<JXPagerViewListViewDelegate>)pagerView:(JXPagerView *)pagerView initListAtIndex:(NSInteger)index {
    
    YWHengVideoListController *listVC = self.listControllerArray[index];
    
    return listVC;
}

#pragma mark - JXCategoryViewDelegate
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    //记录当前选中
//    self.categorySelectedIndex = index;
//    YWHomeListParentVC *vc = self.listControllerArray[self.categorySelectedIndex];
//    self.selectedListVC = vc;
////    假如还没有加载过的就去加载
//    if (!vc.isLoad) {
//        [vc beginLoadData];
//    }
    
#warning - 还是有点问题、、、切换到VC2后滚动播放依然播放VC1的
    //假如非头部播放，你只要切换我就停止播放你，但是不让你自动播放新的，等你滑动屏幕的时候才触发播放新的
    if (self.vedioView.videoStatusType != YWVideoLocationType_headerView) {
        
        [self.vedioView stopPlay];
        self.vedioBgView.hidden = YES;
    }
    
    
    
    
    
}

//JXPagerMainTableView的代理方法
- (BOOL)mainTableViewGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    //禁止banner左右滑动的时候，上下和左右都可以滚动
    if (otherGestureRecognizer == self.headerView.listCollectionView.panGestureRecognizer) {
        return NO;
    }
    //禁止categoryView左右滑动的时候，上下和左右都可以滚动
    if (otherGestureRecognizer == self.categoryView.collectionView.panGestureRecognizer) {
        return NO;
    }
    return [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}



//全屏返回手势方法（写了下面的触摸点在子VC也能全屏手势返回）
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = YES;
    
    //因为方法里面它有if(self.vedioView.isPlaying_containPause == NO)这个判断、所以可以大胆使用、、、然后不需要之前播放哪个才播放哪个、因为它有headerView左VC右VC、所以按范围播放就行了
    [self scrollToStartPlay:[UIScrollView new]];
    
    
     self.isGoToHengVideoDetailVCToDisAppearView = NO;
    
    //把这个搬去了视频开始播放代理那里才可以旋转
    //让整个程序都可以旋转
//    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    appDelegate.allowRotate = 1;

}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = NO;
    
    //这个无论是不是去横视频界面都去隐藏vedioBgView
    self.vedioBgView.hidden = YES;
    
    
    
    //假如是因为去横视频详情而离开本界面的不要去停止vedioView、、、去横视频VC可以旋转
    if (self.isGoToHengVideoDetailVCToDisAppearView) {
        return;
    }
    
    [self.vedioView stopPlay];
    
    //让整个程序不能旋转
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.allowRotate = 0;
    
}

- (Class)scrollViewClassInlistContainerViewInPagerView:(JXPagerView *)pagerView {
    return [FullScreenGestureScrollView class];
}





//// ------------

#pragma mark - 横屏模式


- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    self.pagerView.frame = self.bgView.bounds;
    
    if (!isLanscape) {
        //在这里处理横屏变竖屏时将cell移动到top
        if (self.vedioView.videoStatusType == YWVideoLocationType_leftVC) {
            
            YWHengVideoListController *vc = self.listControllerArray[0];
            [vc scrollToIndexRowToTop:self.vedioView.index];
        }
        else  if (self.vedioView.videoStatusType == YWVideoLocationType_rightVC) {
            
            YWHengVideoListController *vc = self.listControllerArray[1];
            [vc scrollToIndexRowToTop:self.vedioView.index];

        }
        
        
    }
    
    
    
    
}
/**
 *  在此方法中修改布局
 */
-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    
    
    // 布局
    if(isLanscape)
    {
        
        self.pagerView.hidden = YES;
        self.navBar.hidden = YES;
        
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            self.view.backgroundColor = [UIColor blackColor];
//        });
        
        self.panGesture.enabled = YES;
        
        
        self.vedioView.bigScaleBtn.selected = YES;
        
        
        if (self.littleWindowVedioView.hidden == YES) { //小窗口不出现就是正在用vedioView
            
            [self.view addSubview:self.vedioBgView];
            
            self.vedioBgView.frame = CGRectMake(0, 0, Wi, He);
            
            self.vedioView.frame = self.vedioBgView.bounds;
            
            self.vedioBgView.layer.cornerRadius = 0;
        }
        else{ //正在使用小窗口
            [self.view addSubview:self.littleWindowVedioView];
            
            self.littleWindowVedioView.frame = CGRectMake(0, 0, Wi, He);
            
            self.littleWindowVedioView.layer.cornerRadius = 0;
            
        }
        
        
    }else
    {
        
        self.pagerView.hidden = NO;
        self.navBar.hidden = NO;
        
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            self.view.backgroundColor = [UIColor whiteColor];
//        });
        
        self.panGesture.enabled = NO;
        
        //加了这句就可以了、因为你把tableView的frame转换的时候tableView还是以横盘的参考对象、假如你写了layoutIfNeeded它就等你变成了竖屏再以竖屏的tableView为参考对象
        [self.view layoutIfNeeded];
        
        
        self.vedioView.bigScaleBtn.selected = NO;
        
        if (self.littleWindowVedioView.hidden == YES) { //小窗口不出现就是正在用vedioView
            
            //因为就算是headerView的vedioView在播放、竖屏后小窗口的宽高也被改变了、所以这里要重新设置小窗口的宽高
            CGFloat w = Wi/2.5;
            CGFloat h = w/1.5;
            self.littleWindowVedioView.width = w;
            self.littleWindowVedioView.height = h;
            
            switch (self.vedioView.videoStatusType) {
                case YWVideoLocationType_headerView:
                {
                    
                    [self.pagerView.mainTableView addSubview:self.vedioBgView];
                    self.vedioBgView.frame = CGRectMake(0, 0, Wi, headerViewVedioViewHeight);
                    self.vedioView.frame = self.vedioBgView.bounds;
                    
                    self.vedioBgView.layer.cornerRadius = 0;
                }
                    break;
                case YWVideoLocationType_leftVC:
                {
                    YWHengVideoListController *vc = self.listControllerArray[0];
                    
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.vedioView.index inSection:0];
                    YWHomeRecommendVedioCell *vedioCell = [vc.tableView cellForRowAtIndexPath:indexPath];
                    
                    //转换父控件所在位置
                    CGRect rects = [vedioCell.contentView convertRect:vedioCell.videoBgView.frame toView:vc.tableView];

                    self.vedioBgView.frame = rects;

                    self.vedioView.frame = self.vedioBgView.bounds;
                    
                    //添加位置
                    [vc.tableView addSubview:self.vedioBgView];
                    
                    self.vedioBgView.layer.cornerRadius = vedioCell.videoBgView.layer.cornerRadius;
                    self.vedioBgView.clipsToBounds = YES;
                    
                    
                    
                    
                }
                    break;
                case YWVideoLocationType_rightVC:
                {
                    YWHengVideoListController *vc = self.listControllerArray[1];
                    
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.vedioView.index inSection:0];
                    YWHomeRecommendVedioCell *vedioCell = [vc.tableView cellForRowAtIndexPath:indexPath];
                    
                    //转换父控件所在位置
                    CGRect rects = [vedioCell.contentView convertRect:vedioCell.videoBgView.frame toView:vc.tableView];

                    self.vedioBgView.frame = rects;

                    self.vedioView.frame = self.vedioBgView.bounds;
                    
                    //添加位置
                    [vc.tableView addSubview:self.vedioBgView];
                    
                    self.vedioBgView.layer.cornerRadius = vedioCell.videoBgView.layer.cornerRadius;
                    self.vedioBgView.clipsToBounds = YES;
                    
                    
                }
                    break;
                    
                default:
                    break;
            }
            
            
        }
        else{ //正在使用小窗口
            
            CGFloat w = Wi/2.5;
            CGFloat h = w/1.5;
            CGFloat y = self.view.height - h - littleWindowInterval - h;
            CGFloat x = Wi - w - littleWindowInterval;
            
            self.littleWindowVedioView.frame = CGRectMake(x, y, w, h);
            
            [self.view addSubview:_littleWindowVedioView];
            
            self.littleWindowVedioView.layer.cornerRadius = 5;
            
        }
        
        
        
        
        
        
        
        
        
    }


}

//放大/缩小
-(void)videoView:(YWVideoView2 *)videoView didClickBigScaleBtn:(UIButton *)btn{
    
    
//    btn.selected = !btn.selected;
    if (self.vedioView.bigScaleBtn.selected) {
           [self setViewOrientation:UIInterfaceOrientationPortrait];
    }
    else{
        
           [self setViewOrientation:UIInterfaceOrientationLandscapeRight];
    }
    
}




//强制横屏或竖屏
-(void)setViewOrientation:(UIInterfaceOrientation)orientation{

    if (@available(iOS 16.0, *)) {
        [UIApplication.sharedApplication.keyWindow.rootViewController setNeedsUpdateOfSupportedInterfaceOrientations];
              
              NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
              UIWindowScene *ws = (UIWindowScene *)array[0];
              UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [UIWindowSceneGeometryPreferencesIOS.alloc initWithInterfaceOrientations:1 << orientation];
              [ws requestGeometryUpdateWithPreferences:geometryPreferences
                                          errorHandler:^(NSError * _Nonnull error) {
              }];
//            @try {
//                NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
//                UIWindowScene *ws = (UIWindowScene *)array[0];
//                Class GeometryPreferences = NSClassFromString(@"UIWindowSceneGeometryPreferencesIOS");
//                id geometryPreferences = [[GeometryPreferences alloc]init];
//
//                if(orientation == UIInterfaceOrientationPortrait) {
//                    [geometryPreferences setValue:@(UIInterfaceOrientationMaskPortrait) forKey:@"interfaceOrientations"];
//                }
//                else {
//                    [geometryPreferences setValue:@(UIInterfaceOrientationMaskLandscapeRight) forKey:@"interfaceOrientations"];
//                }
//
//                SEL sel_method = NSSelectorFromString(@"requestGeometryUpdateWithPreferences:errorHandler:");
//                void (^ErrorBlock)(NSError *err) = ^(NSError *err){
//                      NSLog(@"调用了Block%@",err);
//                };
//                if ([ws respondsToSelector:sel_method]) {
//                    (((void (*)(id, SEL,id,id))[ws methodForSelector:sel_method])(ws, sel_method,geometryPreferences,ErrorBlock));
//                }
//            } @catch (NSException *exception) {
//                NSLog(@"*****************************华丽分割线**************************");
//                NSLog(@"%@",exception);
//            } @finally {
//
//            }
        } else {
            
            if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
                SEL selector = NSSelectorFromString(@"setOrientation:");
                NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
                [invocation setSelector:selector];
                [invocation setTarget:[UIDevice currentDevice]];
                int val = (int)orientation;
                [invocation setArgument:&val atIndex:2];
                [invocation invoke];

            }

        }
    
    

}


//////

// 本VC可以支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

// 本VC是否支持设备自动旋转
- (BOOL)shouldAutorotate
{
    return YES;
}

/**
 *  当屏幕即将旋转的时候调用
 *  @param toInterfaceOrientation 旋转完毕后的最终方向
 *  @param duration  旋转动画所花费的时间
 */
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

    
    //切换屏幕方向时候把videoFunctionView设为透明效果会好点
//    self.videoFunctionView.alpha = 0;

    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {// 横屏

    } else {//竖屏

    }

}

//开始成功代理
-(void)videoViewDidStartPlay:(YWVideoView2 *)videoView{
    
    //让整个程序能旋转
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.allowRotate = 1;
    
    
}

//停止成功代理
-(void)videoViewDidSotpPlay:(YWVideoView2 *)videoView{
    
    //让整个程序不能旋转
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.allowRotate = 0;
    
    
}


//VC刚出现的时候的显示方向（当前ViewController必须是通过模态出来的UIViewController（模态带导航的无效）方式展现出来的，才会调用这个方法）[新浪财经APP present弹出横屏的K线图应该用这个]
//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
//{
//    return UIInterfaceOrientationLandscapeRight;
//}




//-(void)viewWillAppear:(BOOL)animated{
//
//     [super viewWillAppear:animated];
//
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
//
//}





@end
